#!/usr/bin/ruby

#####################################
#        iptctl.rb                                                                         
#        description;                                                                    
#            provide standadard method                                       
#            to control and access iptables methods                      
#        version           0.9                                                            
#        last modified     08/07/03                                                 
#        please see the file COPYRIGHT for licensing details          
######################################


class Iptctl
    def initialize(ipt_loc="/sbin/iptables", ips_loc="/sbin/iptables-save", ipr_loc="/sbin/iptables-restore")
        @ipt=ipt_loc
        @grep="/bin/grep"
        @ipt_sve=ips_loc
        @ipt_rst=ipr_loc
        
    end
    
    def flush_all(table="all")
        # flush all rules, delete all user defined chains, and reset all policies to ACCEPT
        tmp=Array.new
        if (table=="all" || table =="filter")
            tmp << `#{@ipt} -t filter -P INPUT ACCEPT`
            tmp << `#{@ipt} -t filter -P OUTPUT ACCEPT`
            tmp << `#{@ipt} -t filter -P FORWARD ACCEPT`
            tmp << `#{@ipt} -t filter -F`
            tmp << `#{@ipt} -t filter -X`
        end
        if (table=="all" || table =="nat")
            tmp << `#{@ipt} -t nat -P PREROUTING ACCEPT`
            tmp << `#{@ipt} -t nat -P POSTROUTING ACCEPT`
            tmp << `#{@ipt} -t nat -P OUTPUT ACCEPT`
            tmp << `#{@ipt} -t nat -F`
            tmp << `#{@ipt} -t nat -X`
        end
        if (table=="all" || table =="mangle")
            tmp << `#{@ipt} -t mangle -P PREROUTING ACCEPT`
            tmp << `#{@ipt} -t mangle -P POSTROUTING ACCEPT`
            tmp << `#{@ipt} -t mangle -P OUTPUT ACCEPT`
            tmp << `#{@ipt} -t mangle -P INPUT ACCEPT`
            tmp << `#{@ipt} -t mangle -F`
            tmp << `#{@ipt} -t mangle -X`
        end
        return tmp
    end
    
    def create(chain, table="filter")
        tmp=`#{@ipt} -t #{table} -N #{chain} 2>&1`
        if tmp != ''
            return "Error creating chain #{chain}, error was #{tmp}"
        end        
    end
    
    def list(chain='', table="filter")
        return `#{@ipt} -t #{table} -L #{chain} -n 2>&1`
    end
    
    def vlist(chain='', table="filter")
        return `#{@ipt} -t #{table} -L #{chain} -n --line-numbers -v 2>&1`
    end

    def flush(chain='', table="filter")
        return `#{@ipt} -t #{table} -F #{chain} 2>&1`
    end
    
    def zero(chain='', table="filter")
        return `#{@ipt} -t #{table} -Z #{chain} 2>&1`
    end

    def policy(chain, target, table="filter")
        return `#{@ipt} -t #{table} -P #{chain} #{target} 2>&1`
    end

    def rename(old, new, table="filter")
        return `#{@ipt} -t #{table} -E #{old} #{new} 2>&1`
    end
    
    def save
        tmp=Array.new
        `#{@ipt_sve}`.each {|line|
            tmp << line
        }
        return tmp
    end

    def restore(tmp)
       r=IO.popen(@ipt_rst,"w")
       tmp.each {|line|
            r.puts line
        }
        r.close
    end
    
    def delchain(chain, table="filter")
        tmp=`#{@ipt}  -t #{table} -X #{chain} 2>&1`
        if tmp != ''
            return "Error deleteing chain #{chain}, error was #{tmp}"
        end        
    end
    
    def append(chain, rule, table="filter")
        tmp=`#{@ipt} -t #{table} -A  #{chain} #{rule} 2>&1`
        if tmp != ''
            return "Error appending rule to chain #{chain}, error was #{tmp}"
        end   
    end
    
    def delete(chain, rule_number, table="filter")
        tmp=`#{@ipt} -t #{table} -D  #{chain} #{rule_number} 2>&1`
        if tmp != ''
            return "Error deleting rule number #{rule_number} from chain #{chain}, error was #{tmp}"
        end   
    end
        
    def insert(chain, rule, rule_number=1, table="filter")
        tmp=`#{@ipt} -t #{table} -I  #{chain} #{rule_number} #{rule} 2>&1`
        if tmp != ''
            return "Error inserting rule into chain #{chain}, error was #{tmp}"
        end   
    end
    
    def replace(chain, rule, rule_number, table="filter")
        tmp=`#{@ipt} -t #{table} -R  #{chain} #{rule_number} #{rule} 2>&1`
        if tmp != ''
            return "Error replacing rule #{rule_number} in chain #{chain}, error was #{tmp}"
        end   
    end
end
